﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using Library.UI.Form;
using QuanLyDiem.Controllers;
using QuanLyDiem.models;
using System.Diagnostics;
namespace QuanLyDiem.ThongKe
{
    public partial class frmThongKeDiemMonHoc : DevExpress.XtraEditors.XtraForm
    {
        public frmThongKeDiemMonHoc()
        {
            InitializeComponent();
        }

        private void ThongKeDiemMonHoc_Load(object sender, EventArgs e)
        {
            gridView1.Config(true, false, false, false, false, false, true, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            Context db = new Context();
            var ID_lops = db.LopHocPhans.Select(o => o.ID_lop).Distinct().ToList();
            var lops = new List<Lop>();
            foreach (var row in ID_lops)
            {
                var lop = db.Lops.Where(p => p.ID_lop == row).First();
                lops.Add(lop);
            }
            cmbLop.DataSource = lops.ToList();
            cmbLop.DisplayMember = "Ten_lop";
            cmbLop.ValueMember = "ID_lop";
            var namhocs = db.LopHocPhans.Select(o => o.Nam_hoc).Distinct().ToList();
            var hockys = db.LopHocPhans.Select(o => o.Hoc_ky).Distinct().ToList();
            cmbNamHoc.DataSource = namhocs.ToList();
            cmbHocKy.DataSource = hockys.ToList();
            reptxtDiemX.Mask.EditMask = "[0-9][.][0-9]";
            reptxtDiemX.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
            reptxtDiemY.Mask.EditMask = "[0-9][.][0-9]";
            reptxtDiemY.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            Context db = new Context();
            var ID_lop_hps = db.LopHocPhans.Where(p => p.ID_lop == int.Parse(cmbLop.SelectedValue.ToString()) && p.ID_mon == int.Parse(cmbMonHoc.SelectedValue.ToString()) && p.Nam_hoc == cmbNamHoc.Text && p.Hoc_ky == int.Parse(cmbHocKy.Text)).Select(o => o.ID_lop_hp).FirstOrDefault();
            if (ID_lop_hps == 0)
            {
                MessageBox.Show("Không có lớp học phần");
            }
            else
            {
                var lophps = db.Diems.Where(p => p.ID_lop_hp == ID_lop_hps).ToList();
                var Listlhp = new List<EXT_Diem>();
                foreach (var row in lophps)
                {
                    var sinhvien = db.HoSoSinhViens.Where(p => p.ID_sv == row.ID_sv).Select(o => new { o.Ho_ten, o.Ma_sv, o.Ngay_sinh }).First();
                    var ext_diem = new EXT_Diem
                    {
                        Ten_sinh_vien = sinhvien.Ho_ten,
                        Ma_sv = int.Parse(sinhvien.Ma_sv),
                        Ngay_sinh = sinhvien.Ngay_sinh.ToString().Substring(0, sinhvien.Ngay_sinh.ToString().IndexOf(" ")),
                        ID_lop_hp = ID_lop_hps,
                        ID_sv = row.ID_sv,
                        Diem_X = row.Diem_X,
                        Diem_Y = row.Diem_Y,
                        Diem_Z = row.Diem_Z
                    };
                    Listlhp.Add(ext_diem);
                }
                eXTDiemBindingSource.DataSource = Listlhp.ToList();
            }
        }

        private void cmbLop_SelectedValueChanged(object sender, EventArgs e)
        {
            int b = 0;
            if (!int.TryParse(cmbLop.SelectedValue.ToString(), out b)) return;
            Context db = new Context();
            var ID_mons = db.LopHocPhans.Where(p => p.ID_lop == int.Parse(cmbLop.SelectedValue.ToString())).Select(o => o.ID_mon).Distinct().ToList();
            var mons = new List<MonHoc>();
            foreach (var row in ID_mons)
            {
                var mon = db.MonHocs.Where(p => p.ID_mon == row).First();
                mons.Add(mon);
            }
            cmbMonHoc.DataSource = mons.ToList();
            cmbMonHoc.DisplayMember = "Ten_mon";
            cmbMonHoc.ValueMember = "ID_mon";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog s = new SaveFileDialog();
            s.Filter = "PDF Files|*.pdf";
            if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                gridControl1.ExportToPdf(s.FileName);
                Process.Start(s.FileName);

            }
        }
    }
}